/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file n32g45x_it.c
 * @author Nations
 * @version v1.0.0
 *
 * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 */

#include "n32g45x_it.h"
#include "commun_uart.h"
#if defined(SP_PROJECT)
    #include "app_task.h"
#endif


/* PRQA S 2740, 2870, 2885, 3383 ++ */

/** @addtogroup N32G45X_StdPeriph_Template
 * @{
 */

/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
 * @brief  This function handles NMI exception.
 */
void NMI_Handler(void)
{
    /* This interrupt is generated when HSE clock fails */

    if (RCC_GetIntStatus(RCC_INT_CLKSSIF) != RESET) {
        /* At this stage: HSE, PLL are disabled (but no change on PLL config) and HSI
            is selected as system clock source */

        /* Enable HSE */
        RCC_ConfigHse(RCC_HSE_ENABLE);

        /* Enable HSE Ready interrupt */
        RCC_ConfigInt(RCC_INT_HSERDIF, ENABLE);

        #ifndef SYSCLK_HSE
        /* Enable PLL Ready interrupt */
        RCC_ConfigInt(RCC_INT_PLLRDIF, ENABLE);
        #endif /* SYSCLK_HSE */

        /* Clear Clock Security System interrupt pending bit */
        RCC_ClrIntPendingBit(RCC_INT_CLKSSIF);

        /* Once HSE clock recover, the HSERDY interrupt is generated and in the RCC INTSTS
           routine the system clock will be reconfigured to its previous state (before
           HSE clock failure) */
    }
}

/**
 * @brief  This function handles Hard Fault exception.
 */
void HardFault_Handler(void)
{
    USER_PRINTF("\r\n\r\n!!! HardFault\r\n\r\n");
    User_SystemReset();

    /* Go to infinite loop when Hard Fault exception occurs */
    while (1) {
    }
}

/**
 * @brief  This function handles Memory Manage exception.
 */
void MemManage_Handler(void)
{
    /* Go to infinite loop when Memory Manage exception occurs */
    while (1) {
    }
}

/**
 * @brief  This function handles Bus Fault exception.
 */
void BusFault_Handler(void)
{
    /* Go to infinite loop when Bus Fault exception occurs */
    while (1) {
    }
}

/**
 * @brief  This function handles Usage Fault exception.
 */
void UsageFault_Handler(void)
{
    /* Go to infinite loop when Usage Fault exception occurs */
    while (1) {
    }
}

/**
 * @brief  This function handles SVCall exception.
 */
void SVC_Handler(void)
{
}

/**
 * @brief  This function handles Debug Monitor exception.
 */
void DebugMon_Handler(void)
{
}

/**
 * @brief  This function handles SysTick Handler.
 */
void SysTick_Handler(void)
{
    swt_TimerCallback();

    SysTick_TimerCallback();
}

/******************************************************************************/
/*                 N32G45X Peripherals Interrupt Handlers                     */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_n32g45x.s).                                                 */
/******************************************************************************/

/**
 * @brief  This function handles CAN1 RX0 Handler.
 */
void USB_LP_CAN1_RX0_IRQHandler(void)
{
    if (can1_driver.IrqHandlerISR) {
        can1_driver.IrqHandlerISR((void *)&can1_driver);
    }
}


/**
 * @brief  This function handles DMA1_Channel1 Handler.
 */
void DMA1_Channel1_IRQHandler(void)
{
    if (DMA_GetIntStatus(DMA1_INT_TXC1, DMA1) != RESET) {
        DMA_ClrIntPendingBit(DMA1_INT_TXC1, DMA1);

        if (adc1_dma_driver.DMA_RxCpltCallback) {
            adc1_dma_driver.DMA_RxCpltCallback((void *)&adc1_dma_driver);
        }
    }
}

/**
 * @brief  This function handles DMA2_Channel5 Handler.
 */
void DMA2_Channel5_IRQHandler(void)
{
    if (DMA_GetIntStatus(DMA2_INT_TXC5, DMA2) != RESET) {
        DMA_ClrIntPendingBit(DMA2_INT_TXC5, DMA2);

        if (adc3_dma_driver.DMA_RxCpltCallback) {
            adc3_dma_driver.DMA_RxCpltCallback((void *)&adc3_dma_driver);
        }
    }
}


/**
 * @brief  This function handles TIM2 Handler.
 */
void TIM2_IRQHandler(void)
{
    if (TIM_GetIntStatus(TIM2, TIM_INT_UPDATE) != RESET) {
        TIM_ClrIntPendingBit(TIM2, TIM_INT_UPDATE);

        Watch_TIMx_Callback();
    }
}


#if (UART_1_CFG_ENABLE)
/**
 * @brief  This function handles USART1 global interrupt request.
 */
void USART1_IRQHandler(void)
{
    USARTx_IRQHandler(UART_CHANNEL_1);
}
#endif

#if (UART_2_CFG_ENABLE)

/**
 * @brief This function handles USART2 global interrupt request.
 */
void USART2_IRQHandler(void)
{
    USARTx_IRQHandler(UART_CHANNEL_2);
}
#endif

#if (UART_3_CFG_ENABLE)
/**
 * @brief This function handles USART3 global interrupt request.
 */
void USART3_IRQHandler(void)
{
    USARTx_IRQHandler(UART_CHANNEL_3);
}

#endif

#if (UART_5_CFG_ENABLE)
/**
 * @brief This function handles UART5 global interrupt request.
 */
void UART5_IRQHandler(void)
{
    USARTx_IRQHandler(UART_CHANNEL_5);
}

#endif

#if (UART_6_CFG_ENABLE)
/**
 * @brief This function handles UART6 global interrupt request.
 */
void UART6_IRQHandler(void)
{
    USARTx_IRQHandler(UART_CHANNEL_6);
}

#endif


/**
 * @brief  External lines EXTI0_IRQHandler.
 */
void EXTI0_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_LINE0) != RESET) {
        EXTI_ClrITPendBit(EXTI_LINE0);

        //!< Wakeup interrupt
        LPM_IntCallback(BKP_DATA_WKUP_FLAG_EXIT_PA0);
    }
}

/**
 * @brief  External lines EXTI4_IRQHandler.
 */
void EXTI4_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_LINE4) != RESET) {
        EXTI_ClrITPendBit(EXTI_LINE4);

    }
}

/**
 * @brief  External lines EXTI9_5_IRQHandler.
 */
void EXTI9_5_IRQHandler(void)
{
    uint32_t EXTI_IMASK = 0;
    uint32_t EXTI_PEND = 0;

    EXTI_IMASK = EXTI->IMASK;
    EXTI_PEND  = EXTI->PEND;

    if (((EXTI_PEND & EXTI_LINE5) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE5) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE5;

    }

    if (((EXTI_PEND & EXTI_LINE6) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE6) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE6;

    }

    if (((EXTI_PEND & EXTI_LINE7) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE7) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE7;

    }

    if (((EXTI_PEND & EXTI_LINE8) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE8) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE8;

    }

    if (((EXTI_PEND & EXTI_LINE9) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE9) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE9;

    }
}

/**
 * @brief  External lines EXTI15_10_IRQHandler.
 */
void EXTI15_10_IRQHandler(void)
{
    __IO uint32_t EXTI_IMASK = 0;
    __IO uint32_t EXTI_PEND = 0;

    EXTI_IMASK = EXTI->IMASK;
    EXTI_PEND  = EXTI->PEND;

    if (((EXTI_PEND & EXTI_LINE10) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE10) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE10;

    }

    if (((EXTI_PEND & EXTI_LINE11) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE11) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE11;

    }

    if (((EXTI_PEND & EXTI_LINE12) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE12) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE12;

    }

    if (((EXTI_PEND & EXTI_LINE13) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE13) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE13;

    }

    if (((EXTI_PEND & EXTI_LINE14) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE14) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE14;

    }

    if (((EXTI_PEND & EXTI_LINE15) != (uint32_t)RESET) && ((EXTI_IMASK & EXTI_LINE15) != (uint32_t)RESET)) {
        EXTI->PEND = EXTI_LINE15;

    }

}

#if (LSI_TIM_MEASURE)

/**
 * @brief  This function handles TIM5 global interrupt request.
 */
void TIM5_IRQHandler(void)
{
    if (TIM_GetIntStatus(TIM5, TIM_INT_CC4) != RESET) {
        #if (LSI_TIM_MEASURE)

        if (LSI_CaptureNumber == 0) {
            /* Get the Input Capture value */
            IC1ReadValue1 = TIM_GetCap4(TIM5);
        } else if (LSI_CaptureNumber == 2) {
            RCC_ClocksType rcc_clks;
            /* Get the Input Capture value */
            IC1ReadValue2 = TIM_GetCap4(TIM5);

            /* Capture computation */
            if (IC1ReadValue2 > IC1ReadValue1) {
                Capture = (IC1ReadValue2 - IC1ReadValue1);
            } else {
                Capture = ((0xFFFF - IC1ReadValue1) + IC1ReadValue2);
            }

            RCC_GetClocksFreqValue(&rcc_clks);

            /* Frequency computation */
            C_LsiFreq = (uint32_t)rcc_clks.Pclk1Freq / Capture;
            C_LsiFreq *= 32;
        }

        LSI_CaptureNumber++;

        #endif

        /* Clear TIM5 Capture compare interrupt pending bit */
        TIM_ClrIntPendingBit(TIM5, TIM_INT_CC4);
    }
}
#endif

/**
 * @brief  This function handles RCC interrupt request.
 */
void RCC_IRQHandler(void)
{
    if (RCC_GetIntStatus(RCC_INT_HSERDIF) != RESET) {
        /* Clear HSERDY interrupt pending bit */
        RCC_ClrIntPendingBit(RCC_INT_HSERDIF);

        /* Check if the HSE clock is still available */
        if (RCC_GetFlagStatus(RCC_FLAG_HSERD) != RESET) {
            #ifdef SYSCLK_HSE
            /* Select HSE as system clock source */
            RCC_ConfigSysclk(RCC_SYSCLK_SRC_HSE);
            #else
            /* Enable PLL: once the PLL is ready the PLLRDY interrupt is generated */
            RCC_EnablePll(ENABLE);
            #endif /* SYSCLK_HSE */
        }
    }

    if (RCC_GetIntStatus(RCC_INT_PLLRDIF) != RESET) {
        /* Clear PLLRDY interrupt pending bit */
        RCC_ClrIntPendingBit(RCC_INT_PLLRDIF);

        /* Check if the PLL is still locked */
        if (RCC_GetFlagStatus(RCC_FLAG_PLLRD) != RESET) {
            /* Select PLL as system clock source */
            RCC_ConfigSysclk(RCC_SYSCLK_SRC_PLLCLK);
        }
    }
}


#if (PVD_ENABLE)
/**
 * @brief  This function handles the PVD Output interrupt request defined in main.h .
 */
void PVD_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_LINE16) != RESET) {
        /* Clear the Key Button EXTI line pending bit */
        EXTI_ClrITPendBit(EXTI_LINE16);

        Error_Handler();
    }
}
#endif

/**
 * @brief  This function handles PPP interrupt request.
 */
/*void PPP_IRQHandler(void)
{
}*/

/**
 * @}
 */
/* PRQA S 2740, 2870, 2885, 3383 -- */
